package com.company.oa.hr.controller.employee;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.company.oa.common.api.Result;
import com.company.oa.hr.dto.employee.ContractDTO;
import com.company.oa.hr.entity.employee.Contract;
import com.company.oa.hr.service.employee.ContractService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/hr/employee/contract")
public class ContractController {

    @Autowired
    private ContractService contractService;

    /**
     * 分页查询员工合同列表
     */
    @GetMapping("/page")
    @PreAuthorize("hasAuthority('hr:employee:contract:list')")
    public Result<IPage<Contract>> getContractPage(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size,
            @RequestParam(required = false) Long employeeId) {
        IPage<Contract> pageResult = contractService.getContractPage(page, size, employeeId);
        return Result.success(pageResult);
    }

    /**
     * 获取合同详情
     */
    @GetMapping("/{id}")
    @PreAuthorize("hasAuthority('hr:employee:contract:info')")
    public Result<Contract> getContractDetail(@PathVariable Long id) {
        Contract contract = contractService.getById(id);
        return Result.success(contract);
    }

    /**
     * 创建员工合同
     */
    @PostMapping
    @PreAuthorize("hasAuthority('hr:employee:contract:create')")
    public Result<Boolean> createContract(@RequestBody @Valid ContractDTO contractDTO) {
        // 从当前登录用户中获取创建人ID
        Long creatorId = 1L; // 实际项目中应从SecurityContext中获取
        boolean success = contractService.createContract(contractDTO, creatorId);
        return Result.success(success);
    }

    /**
     * 更新员工合同
     */
    @PutMapping
    @PreAuthorize("hasAuthority('hr:employee:contract:update')")
    public Result<Boolean> updateContract(@RequestBody @Valid ContractDTO contractDTO) {
        boolean success = contractService.updateContract(contractDTO);
        return Result.success(success);
    }

    /**
     * 删除员工合同
     */
    @DeleteMapping("/{id}")
    @PreAuthorize("hasAuthority('hr:employee:contract:delete')")
    public Result<Boolean> deleteContract(@PathVariable Long id) {
        boolean success = contractService.deleteContract(id);
        return Result.success(success);
    }

    /**
     * 上传合同附件
     */
    @PostMapping("/upload")
    @PreAuthorize("hasAuthority('hr:employee:contract:upload')")
    public Result<String> uploadContractAttachment(@RequestParam("file") MultipartFile file) {
        String filePath = contractService.uploadContractAttachment(file);
        return Result.success(filePath);
    }

    /**
     * 获取员工所有合同
     */
    @GetMapping("/employee/{employeeId}")
    @PreAuthorize("hasAuthority('hr:employee:contract:list')")
    public Result<List<Contract>> getEmployeeContracts(@PathVariable Long employeeId) {
        List<Contract> contracts = contractService.getEmployeeContracts(employeeId);
        return Result.success(contracts);
    }
}